--
-- SpeedDungeonInfo
-- 竞速副本通关信息

local fileName = string.getModeName(...)
local ClassRef = class(fileName)

local rewardLv = {
	["NONE"] = 0,
	["BRONZE"] = 1,
	["SILVER"] = 2,
	["GOLD"] = 3,
}

-- //奖励档位
-- enum REWARD{
-- 	NONE	= 0;//无
-- 	BRONZE	= 1;//铜
-- 	SILVER	= 2;//银
-- 	GOLD	= 3;//金
-- }
-- message SpeedDungeonAchievement {
--    int32 target = 1; //竞速副本标识
--    int32 sec = 2;//最佳通关时间
--    REWARD rewarded = 3;//已领取的最高奖励档位
-- }
function ClassRef:ctor(data)
	if data then
		self:init(data)
	end
end

function ClassRef:init(data)
	if not self._conf or (self._conf and self.target ~= data.target) then
		self._conf = GD:queryDungeonRaceModeById( data.target )
	end

	self.target = data.target
	self.bestRecord = data.sec
	self.bestReward = data.rewarded
	self.bestRewardLv = protobuf.enum_id("pb_speeddungeon.REWARD", data.rewarded)
end

function ClassRef:updateRecord(record)
	-- 未初始化时为0
	if self.bestRecord == 0 or record < self.bestRecord then
		self.bestRecord = record
	end
end

function ClassRef:updateReward(rewards)
	for i,reward in ipairs(rewards) do
		local lv = protobuf.enum_id("pb_speeddungeon.REWARD", reward)
		if lv > self.bestRewardLv then
			self.bestReward = reward
			self.bestRewardLv = lv
		end
	end
end

function ClassRef:getDesc()
	return L(self._conf.dungeonTips)
end

function ClassRef:getIconBg()
	if not self._iconBg then
		self._iconBg = Res.teamtarget_pic .. self._conf.dungeonBg .. ".png"
	end
	return self._iconBg
end

function ClassRef:getRankIcon(lv)
	-- 资源名字需要经过转换
	lv = lv or self.bestRewardLv
	local index = 4 - lv
	if index == 4 then
		index = 0
	end
	local ary = {Res.teamUIPath, "team_trophy_icon_", index, ".png"}
	return table.concat(ary)
end

function ClassRef:getGrades()
	if not self._grades then
		local grades = {}
		local grade
		for i=1,3 do
			grade = {}
			grade.time = tonumber(self._conf["timeline" .. i])
			grade.rewards = UD:parseItemsFromString(self._conf["reward" .. i])
			grades[i] = grade
		end
		self._grades = grades
	end

	for i,grade in ipairs(self._grades) do
		grade.isGot = i <= self.bestRewardLv
	end

	return self._grades
end

function ClassRef:getGradeByLv(lv)
	local grades = self:getGrades()
	return grades[lv]
end

-- 返回最快的通关时间
function ClassRef:getBestRecord()
	return  self.bestRecord
end

-- 返回最好成绩对应的奖励等级
function ClassRef:getSpeedDungeonBestRewardLv()
	return self.bestRewardLv
end

-- tempLv 对应奖励领取与否？
function ClassRef:hasRetrievedRewardByLv( tempLv )
	return tempLv <= self.bestRewardLv
end

return ClassRef
